package com.kbbywyclient.presenter;

import android.content.Context;

import com.kbbywyclient.R;
import com.kbbywyclient.data.model.MobileLoginOfRegistModel;
import com.kbbywyclient.data.model.RegistUserModel;
import com.kbbywyclient.data.model.UserModel;
import com.kbbywyclient.presenter.base.BasePresenter;
import com.kbbywyclient.service.ApiService;
import com.kbbywyclient.service.ApiServiceHelper;
import com.kbbywyclient.ui.contarct.LoginContract;
import com.kbbywyclient.util.HttpExcUtil;
import com.kbbywyclient.util.RsaJsonUtil;
import com.kbbywyclient.util.ToastUtil;

import javax.inject.Inject;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Description:
 * Created by FHW
 * on 2017/3/30
 */

public class LoginPresenter extends BasePresenter implements LoginContract.Presenter {

    @Inject
    Context mContext;
    @Inject
    ApiService apiService;
    @Inject
    RsaJsonUtil rsaJsonUtil;

    private LoginContract.View view;

    @Inject
    public LoginPresenter(LoginContract.View view) {
        this.view = view;

    }

    @Override
    public void getUserLogin(String UserName, String PassWord, String Device_Token, String APPType) {
        rsaJsonUtil.put("UserName", UserName);
        rsaJsonUtil.put("PassWord", PassWord);
        rsaJsonUtil.put("Device_Token", Device_Token);
        rsaJsonUtil.put("APPType", APPType);

        view.showLoadingDialog(mContext.getString(R.string.text_logining));
        ApiServiceHelper.
                appointThreadNormal(apiService.getUserLogin(rsaJsonUtil.getRsaParams())).
                subscribe(new Observer<UserModel>() {
                    @Override
                    public void onCompleted() {
                        view.closeLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        HttpExcUtil.printError(e);
                        view.closeLoadingDialog();
                    }

                    @Override
                    public void onNext(UserModel userModel) {
                        if (userModel.getData() != null) {
                            view.loadSucUserLogin(userModel.getData());
                        } else {
                            ToastUtil.showShort(userModel.getMsg());
                        }
                    }
                });
    }

    @Override
    public void sendOfCheckMobileCode(String Mobile, String Type) {
        rsaJsonUtil.put("Mobile", Mobile);
        if (!Type.isEmpty()) {
            rsaJsonUtil.put("Type", Type);
            view.showLoadingDialog(mContext.getString(R.string.text_loading));
        }
        ApiServiceHelper.
                appointThreadNormal(apiService.sendOfCheckMobileCode(rsaJsonUtil.getRsaParams())).
                subscribe(new Observer<RegistUserModel>() {
                    @Override
                    public void onCompleted() {
                        view.closeLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        HttpExcUtil.printError(e);
                        view.closeLoadingDialog();
                    }

                    @Override
                    public void onNext(RegistUserModel registUserModel) {
                        if (!Type.isEmpty()) {
                            view.loadSucCheck(registUserModel.getData());
                        } else {
                            view.loadSucCode(registUserModel.getData());
                        }
                    }
                });
    }

    @Override
    public void getMobileCodeLoginOfRegist(String SalesID, String Mobile, String Code, String Device_Token, String APPType, String CityName) {
        rsaJsonUtil.put("SalesID", SalesID);
        rsaJsonUtil.put("Mobile", Mobile);
        rsaJsonUtil.put("Code", Code);
        rsaJsonUtil.put("Device_Token", Device_Token);
        rsaJsonUtil.put("APPType", APPType);
        if (!CityName.isEmpty()) {
            rsaJsonUtil.put("CityName", CityName);
        }
        view.showLoadingDialog(mContext.getString(R.string.text_logining));
        ApiServiceHelper.
                appointThreadNormal(apiService.getMobileCodeLoginOfRegist(rsaJsonUtil.getRsaParams())).
                subscribe(new Observer<MobileLoginOfRegistModel>() {
                    @Override
                    public void onCompleted() {
                        view.closeLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        HttpExcUtil.printError(e);
                        view.closeLoadingDialog();
                    }

                    @Override
                    public void onNext(MobileLoginOfRegistModel mobileLoginOfRegistModel) {
                        view.loadSucLoginOfRegist(mobileLoginOfRegistModel.getData());
                    }
                });
    }

    @Override
    public void checkMobile(String Mobile) {
        rsaJsonUtil.put("Mobile", Mobile);
//        if (!Type.isEmpty()) {
//            rsaJsonUtil.put("Type", Type);
        view.showLoadingDialog(mContext.getString(R.string.text_loading));
//        }
        ApiServiceHelper.
                appointThreadNormal(apiService.checkMobile(rsaJsonUtil.getRsaParams())).
                subscribe(new Observer<RegistUserModel>() {
                    @Override
                    public void onCompleted() {
                        view.closeLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        HttpExcUtil.printError(e);
                        view.closeLoadingDialog();
                    }

                    @Override
                    public void onNext(RegistUserModel registUserModel) {
//                        if (!Type.isEmpty()) {
                        view.loadSucCheck(registUserModel.getData());
//                        } else {
//                            view.loadSucCode(registUserModel.getData());
//                        }
                    }
                });
    }

    @Override
    public void sendMobileCodeNews(String Mobile) {
        rsaJsonUtil.put("Mobile", Mobile);
        view.showLoadingDialog(mContext.getString(R.string.text_loading));
        ApiServiceHelper.
                appointThreadNormal(apiService.sendMobileCodeNews(rsaJsonUtil.getRsaParams())).
                subscribe(new Observer<RegistUserModel>() {
                    @Override
                    public void onCompleted() {
                        view.closeLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        HttpExcUtil.printError(e);
                        view.closeLoadingDialog();
                    }

                    @Override
                    public void onNext(RegistUserModel registUserModel) {
                        view.loadSucCode(registUserModel.getData());
                    }
                });
    }

}
